package com.example.viewdemo.androidThreadTest

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.example.viewdemo.databinding.ActivityAndroidThreadTestBinding
import kotlin.concurrent.thread

class AndroidThreadTest : AppCompatActivity(), View.OnClickListener {
    private lateinit var binding: ActivityAndroidThreadTestBinding

    val updateText = 1
    val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            // 在这里可以进行UI操作
            when (msg.what) {
                updateText -> binding.textView9.text = "Nice to meet you"
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding= ActivityAndroidThreadTestBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.changeTextBtn.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when(v){
            binding.changeTextBtn->{
                thread {
                    val msg = Message()
                    msg.what = updateText
                    handler.sendMessage(msg) // 将Message对象发送出去
                }
            }
        }
    }
}